﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Tarea4
{
    class ModelNNInteractive : ModelBase
    {
        /// <summary>
        /// A the stack of visited states.
        /// </summary>
        private Stack<StateBase> statesStack;

        private StateBase state;

        override public void Initialize(int sleepInterval, short[,] initialBoard)
        {
            base.Initialize(sleepInterval, initialBoard);
            statesStack = new Stack<StateBase>();
            state = new StateBase(initialBoard);
        }

        override protected void ModelStart()
        {
            ///VOID METHOD, NO ALGORITHM IN INTERACTIVE MODE!
        }

        override public int MoveCell(int row, int col)
        {
            StateBase tmp = state.GetNewState(row, col);

            if (tmp == null) return -1;

            statesStack.Push(state);
            state = tmp;

            if (state.IsGoalState())
                NotifyAll(this.state, "You have reached the goal state!!!", "Cell ("+row+","+col+") has been moved. Depth " + state.Index);
            else
                NotifyAll(this.state, "", "Cell (" + row + "," + col + ") has been moved. Depth " + state.Index);

            return this.state.Index;
        }

        override public int UnDoMoveCell()
        {
            int index = 0;

            try
            {
                index = state.Index;
                state = statesStack.Pop();
                NotifyAll(state, "Undo.", "Movement "+index+" has been undone.");

                return index;
            }
            catch(InvalidOperationException e) { }

            NotifyAll(state, "Undo.", "Cannot undo first state.");
            return -1;
        }

        override public void Start() { }

        override public void Pause() { }

        override public void Stop() { }  
    }
}
